package drawing

import (
	"fmt"
	"image"
	"image/color"
	"image/jpeg"
	"math"
	"os"
	"robot/study/LiveRobot/define"
	"robot/study/LiveRobot/sdk/zlog"
	"strconv"
	"time"
)

//import zlog "../../sdk/zlog"

type Drawing struct {
	Msg *define.Message2
}

func (draw *Drawing) Test() {
	fmt.Printf("test")
}

const (
	dx = 650
	dy = 400
)

type Putpixel func(x, y int)

func Test() {
	fileName := strconv.Itoa(int(time.Now().Unix())) + ".png"
	file, err := os.Create(fileName)
	if err != nil {
		zlog.Error(err)
	}
	defer func(file *os.File) {
		err := file.Close()
		if err != nil {

		}
	}(file)
	naga := image.NewNRGBA(image.Rect(0, 0, dx, dy))
	drawline(1, 1, dx-2, dy-2, func(x, y int) {

		naga.Set(x, y, color.RGBA{R: uint8(x), G: uint8(y), A: 255})
	})
	for y := 0; y < dy; y++ {
		naga.Set(1, y, color.White)
		naga.Set(dx-1, y, color.White)
	}
	err = jpeg.Encode(file, naga, &jpeg.Options{Quality: 100}) //图像质量值为100，是最好的图像显示
	if err != nil {
		zlog.Error(err)
	}

}

func int64Abs(x int64) int64 {
	y := x >> 63
	x = x ^ y - y
	return x
}

func Pic(dx, dy int) [][]uint8 {
	pic := make([][]uint8, dx)
	for i := range pic {
		pic[i] = make([]uint8, dy)
		for j := range pic[i] {
			pic[i][j] = uint8(i * j % 255)
		}
	}
	return pic
}

func drawline(x0, y0, x1, y1 int, brush Putpixel) {
	dx := math.Abs(float64(x1 - x0))
	dy := math.Abs(float64(y1 - y0))
	sx, sy := 1, 1
	if x0 >= x1 {
		sx = -1
	}
	if y0 >= y1 {
		sy = -1
	}
	err := dx - dy
	for {
		brush(x0, y0)
		if x0 == x1 && y0 == y1 {
			return
		}
		e2 := err * 2
		if e2 > -dy {
			err -= dy
			x0 += sx
		}
		if e2 < dx {
			err += dx
			y0 += sy
		}
	}
}
